package com.cskaoyan.service.marketmanage;

import com.cskaoyan.mapper.MarketRegionMapper;
import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.pojo.MarketRegion;
import com.cskaoyan.module.pojo.MarketRegionExample;
import com.cskaoyan.module.vo.MarketManagerAdministrativeRegionsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MarketManagerServiceImpl implements MarketManagerService {
    @Autowired
    MarketRegionMapper marketRegionMapper;

    @Override
    public BaseRespVo getAdminRegionList() {
        MarketManagerAdministrativeRegionsVO marketManagerAdministrativeRegionsVO = new MarketManagerAdministrativeRegionsVO();
        //"total":31,"pages":1,"limit":31,"page":1,"
        marketManagerAdministrativeRegionsVO.setLimit(31);
        marketManagerAdministrativeRegionsVO.setPage(1);
        marketManagerAdministrativeRegionsVO.setPages(1);
        marketManagerAdministrativeRegionsVO.setTotal(31);
        //List<ListDTO> list;
        ArrayList<MarketManagerAdministrativeRegionsVO.ListDTO> listDTOS = new ArrayList<>();

        MarketRegionExample marketRegionExample = new MarketRegionExample();
        Integer i=1;
        marketRegionExample.createCriteria().andTypeEqualTo(i.byteValue());
        List<MarketRegion> marketRegions = marketRegionMapper.selectByExample(marketRegionExample);
        for (MarketRegion marketRegion : marketRegions) {
            MarketManagerAdministrativeRegionsVO.ListDTO listDTO = new MarketManagerAdministrativeRegionsVO.ListDTO();
            listDTO.setId(marketRegion.getId());
            listDTO.setName(marketRegion.getName());
            listDTO.setCode(marketRegion.getCode());

            Byte type = marketRegion.getType();
            listDTO.setType(type.intValue());
            listDTOS.add(listDTO);
        }
        marketManagerAdministrativeRegionsVO.setList(listDTOS);
        for (MarketManagerAdministrativeRegionsVO.ListDTO listDTO : listDTOS) {
            MarketRegionExample example1 = new MarketRegionExample();
            example1.createCriteria().andPidEqualTo(listDTO.getId());
            List<MarketRegion> marketRegions1 = marketRegionMapper.selectByExample(example1);
            ArrayList<MarketManagerAdministrativeRegionsVO.ListDTO.ChildrenDTO> childrenDTOS = new ArrayList<>();
            for (MarketRegion marketRegion : marketRegions1) {
                MarketManagerAdministrativeRegionsVO.ListDTO.ChildrenDTO childrenDTO = new MarketManagerAdministrativeRegionsVO.ListDTO.ChildrenDTO();
                childrenDTO.setName(marketRegion.getName());
                childrenDTO.setCode(marketRegion.getCode());
                Byte type = marketRegion.getType();
                childrenDTO.setType(type.intValue());
                childrenDTO.setId(marketRegion.getId());
                childrenDTOS.add(childrenDTO);
            }
            listDTO.setChildren(childrenDTOS);
            for (MarketManagerAdministrativeRegionsVO.ListDTO.ChildrenDTO dto : childrenDTOS) {
                MarketRegionExample example  = new MarketRegionExample();
                example.createCriteria().andPidEqualTo(dto.getId());
                List<MarketRegion> marketRegions2 = marketRegionMapper.selectByExample(example);
                    ArrayList<MarketManagerAdministrativeRegionsVO.ListDTO.ChildrenDTO.ChildrenDTO2> childrenDTO2s = new ArrayList<>();
                for (MarketRegion marketRegion : marketRegions2) {
                    MarketManagerAdministrativeRegionsVO.ListDTO.ChildrenDTO.ChildrenDTO2 childrenDTO2 =
                            new MarketManagerAdministrativeRegionsVO.ListDTO.ChildrenDTO.ChildrenDTO2();
                    childrenDTO2.setCode(marketRegion.getCode());
                    childrenDTO2.setId(marketRegion.getId());
                    childrenDTO2.setName(marketRegion.getName());
                    Byte type = marketRegion.getType();
                    childrenDTO2.setType(type.intValue());
                    childrenDTO2s.add(childrenDTO2);
                }
                dto.setChildren(childrenDTO2s);
            }



        }

        return BaseRespVo.ok(marketManagerAdministrativeRegionsVO);
    }
}
